package com.jobsleaf.propertytester;

import org.eclipse.core.runtime.ListenerList;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.ISelectionProvider;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.ViewPart;
import org.eclipse.ui.services.IEvaluationService;

public class PropertTesterView extends ViewPart implements ISelectionProvider
{
	private String selection = "ashok";
	private TableViewer mTableViewer;

	public void createPartControl(Composite parent)
	{
		mTableViewer = new TableViewer(parent, SWT.FULL_SELECTION | SWT.H_SCROLL | SWT.V_SCROLL | SWT.SINGLE | SWT.MULTI | SWT.CHECK);
		mTableViewer.setContentProvider(new TableContentProvider());
		mTableViewer.setLabelProvider(new TableLabelProvider());
		String[] COLUMN_NAME = { "Roll No", "Student Name", "Student Age", "Date Of Birth", "Father Name" };
		Table table = mTableViewer.getTable();
		for (int i = 0; i < COLUMN_NAME.length; i++)
		{
			TableColumn tableColumn = new TableColumn(table, SWT.BOLD);
			tableColumn.setText(COLUMN_NAME[i]);
			// tableColumn.setAlignment(SWT.LEFT);
			tableColumn.setWidth(100);
		}
		mTableViewer.setInput(TableUtility.getStudentList());
		table.setLinesVisible(true);
		table.setHeaderVisible(true);
		getSite().setSelectionProvider(mTableViewer);
		hookPopupMenu();

		mTableViewer.addSelectionChangedListener(new ISelectionChangedListener()
		{
			public void selectionChanged(SelectionChangedEvent event)
			{
				updatePropertyTester();
			}
		});
	}

	ListenerList listeners = new ListenerList();

	public void setFocus()
	{
	}

	private void hookPopupMenu()
	{
		MenuManager menuManager = new MenuManager();
		Menu menu = menuManager.createContextMenu(mTableViewer.getControl());
		mTableViewer.getControl().setMenu(menu);
		getSite().registerContextMenu(menuManager, mTableViewer);
		getSite().setSelectionProvider(this);
	}

	public void addSelectionChangedListener(ISelectionChangedListener listener)
	{
		listeners.add(listener);
	}

	public ISelection getSelection()
	{
		return new StructuredSelection(selection);
	}

	public void removeSelectionChangedListener(ISelectionChangedListener listener)
	{
		listeners.remove(listener);
	}

	public void setSelection(ISelection select)
	{
		Object[] list = listeners.getListeners();
		for (int i = 0; i < list.length; i++)
		{
			((ISelectionChangedListener) list[i]).selectionChanged(new SelectionChangedEvent(this, select));
		}
	}

	private void updatePropertyTester()
	{
		int count = 0;
		Integer noOfRowsChecked = null;
		TableItem tableItem[] = mTableViewer.getTable().getItems();
		for (TableItem item : tableItem)
		{
			if (item.getChecked())
			{
				count++;
			}
		}
		noOfRowsChecked = count;
		IEvaluationService evaludationService = (IEvaluationService) PlatformUI.getWorkbench().getActiveWorkbenchWindow().getService(IEvaluationService.class);
		evaludationService.getCurrentState().addVariable("noOfRowsChecked", noOfRowsChecked);
		evaludationService.requestEvaluation("com.jobsleaf.propertytester.canDeleteItem");
	}
}
